package com.odm.communitymanage.util

import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.UriUtils
import com.odm.communitymanage.core.Constants
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import com.zhihu.matisse.internal.entity.CaptureStrategy
import java.io.File

/**
 * @description: 工具类
 * @author: ODM
 * @date: 2020/4/16
 */
object Utils {

    fun getImageManagerIntent():Intent{
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        return  intent
    }

    fun openMatissePage(fragment : Fragment){
        Matisse.from(fragment)
            .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.WEBP))
            .countable(true)
            .maxSelectable(3)
            .capture(true)
            .captureStrategy( CaptureStrategy(true,"com.odm.communitymanage.fileprovider"))
            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
            .thumbnailScale(0.55f)
            .imageEngine(GlideEngine())
            .showPreview(false)
            .forResult(Constants.REQUEST_IMAGE_CODE)
    }


    fun openMatissePage(activity : Activity){
        Matisse.from(activity)
            .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.WEBP))
            .countable(true)
            .maxSelectable(3)
            .capture(true)
            .captureStrategy( CaptureStrategy(true,"com.odm.communitymanage.fileprovider"))
            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
            .thumbnailScale(0.55f)
            .imageEngine(GlideEngine())
            .showPreview(false)
            .forResult(Constants.REQUEST_IMAGE_CODE)
    }

    /**
     *
     * @param uri Uri
     * @param toCompress Boolean 是否对Uri转换后的File进行压缩
     * @return File 生成的目标file
     */
    public fun getPhotoFileByUri(uri : Uri, toCompress: Boolean ) : File {
        var uriPath : String ?= uri.path ?:""
        val photo =  if(uriPath?.startsWith("/my_images") == true){
            uriPath = uriPath.substringAfter("/my_images")
            File(uriPath)
        }else {
            UriUtils.uri2File(uri)
        }
        if(toCompress){
            photo?.let {
                // 压缩图片File
                ImageCompressUtil.getInstance().compressImageFile(photo)
            }
        }
        return photo
    }


}